package cn.nju.sirx.test.algorithm.sort;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

public class Util {

    public static Util getInstance() {
        if (instance == null) {
            instance = new Util();
        }
        return instance;
    }

    /**
     * Check whether the given array is ordered ASC.
     * e.g. 1,3,4,5,6,8...
     * 
     * @param <T> array element type
     * @param data array to check
     * @return whether the given array is ordered ASC
     */
    public static <T extends Comparable<T>> boolean isASC(T[] data) {
        for (int i = 0; i < data.length - 1;) {
            if (data[i++].compareTo(data[i]) > 0) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * Check whether the given file's words are ordered ASC.
     * e.g. 1,3,4,5,6,8...
     * The file should follow the format with each word in a single line.
     * For example:
     * --------------------
     * word1
     * word2
     * word3
     * ...
     * -------------------- 
     * 
     * @param file file to check
     * @return whether the given file's words are ordered ASC
     * @throws IOException when IO exception occurs
     */
    public static boolean isASC(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String preWord = reader.readLine();
        String line;
        try{
            while ((line = reader.readLine()) != null) {
                if (line.compareTo(preWord) < 0) {
                    return false;
                }
                preWord = line;
            }
            return true;
        }
        finally{
            reader.close();
        }
    }

    /**
     * Get an array of random integers.
     * 
     * @param size array size
     * @return an array of random integers
     */
    public static Integer[] getIntegers(int size) {
        Random random = new Random();
        Integer[] result = new Integer[size];
        for (int i = 0; i < result.length; i++) {
            result[i] = random.nextInt(size * 10);
        }
        return result;
    }

    /**
     * Get an temporary file for external testing.
     * Remember to delete the file manually.
     * 
     * @param wordCount word count in the file
     * @return an temporary file for external testing
     * @throws IOException when IO exception occurs
     */
    public static File getExternalTestFile(int wordCount) throws IOException {
        // File file = File.createTempFile(prefix, suffix)
        File file = new File(String.valueOf(System.currentTimeMillis()));
        PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
        Random random = new Random();
        int zeroCount = 2, tmp = wordCount;
        while ((tmp = tmp / 10) != 0) {
            zeroCount++;
        }
        String format = "%0" + zeroCount + "d";
        wordCount *= 10;
        for (int i = 0; i < wordCount; i++) {
            String randInt = String.format(format, random.nextInt(wordCount));
            writer.print(randInt);
            writer.print('\n');
            System.out.println(randInt);
        }
        writer.close();
        System.out.println(file.length());
        return file;
    }

    private Util() {
    }

    private static Util instance;

}
